// Copyright (c) 2022 Presto Labs Pte. Ltd.
// Author: gekim
#include <iostream>

#include <fmt/format.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include "appcoin2/support/feed/py_fastfeed_reader.h"

namespace appcoin2::support::feed {

namespace py = pybind11;

PyFastfeedReader::PyFastfeedReader(const std::string& mear_str,
                                   const std::vector<std::string>& norms,
                                   const std::string& machine,
                                   const std::string& root_dir,
                                   const std::string& interval_start,
                                   const std::string& interval_end) {
  mear_ = MarketExchangeApiRecipe::FromString(mear_str);
  machine_ = machine;
  root_dir_ = root_dir;
  for(auto norm : norms) {
    product_config_.add_norms(norm);
  }

  fastfeed_reader_.reset(
      new FastFeedReader(mear_, product_config_, machine_, root_dir_, 0, 1, 0,
                         TimestampFromString(interval_start),
                         TimestampFromString(interval_end), 524288));
}

py::tuple PyFastfeedReader::load(const fastfeed::proto::FeedType& feed_type) {
  if (fastfeed_reader_ && fastfeed_reader_->Read(&feed_)) {
    if (feed_.feed_type() == fastfeed::proto::FEED_TYPE_METADATA ||
        feed_.feed_type() == feed_type) {
      return py::make_tuple(true, feed_.ShortDebugString());
    } else {
      return py::make_tuple(true, nullptr);
    }
  }
  return py::make_tuple(false, nullptr);
}

PYBIND11_MODULE(py_fastfeed_reader, m) {
  py::class_<PyFastfeedReader>(m, "PyFastfeedReader")
      .def(py::init<const std::string&, 
                    const std::vector<std::string>&,
                    const std::string&,
                    const std::string&,
                    const std::string&,
                    const std::string&>())
      .def("load", &PyFastfeedReader::load);

  py::enum_<fastfeed::proto::FeedType>(m, "FeedType")
      .value("FEED_TYPE_INVALID", fastfeed::proto::FEED_TYPE_INVALID)
      .value("FEED_TYPE_METADATA", fastfeed::proto::FEED_TYPE_METADATA)
      .value("FEED_TYPE_BOOK", fastfeed::proto::FEED_TYPE_BOOK)
      .value("FEED_TYPE_TRADE", fastfeed::proto::FEED_TYPE_TRADE)
      .value("FEED_TYPE_TICKER", fastfeed::proto::FEED_TYPE_TICKER)
      .value("FEED_TYPE_LIQUIDATION_BOOK",
             fastfeed::proto::FEED_TYPE_LIQUIDATION_BOOK)
      .value("FEED_TYPE_LIQUIDATION_ORDER",
             fastfeed::proto::FEED_TYPE_LIQUIDATION_ORDER)
      .value("FEED_TYPE_OPEN_INTEREST",
             fastfeed::proto::FEED_TYPE_OPEN_INTEREST)
      .value("FEED_TYPE_INDEX", fastfeed::proto::FEED_TYPE_INDEX)
      .value("FEED_TYPE_MARK_PRICE", fastfeed::proto::FEED_TYPE_MARK_PRICE)
      .value("FEED_TYPE_FUNDING_RATE", fastfeed::proto::FEED_TYPE_FUNDING_RATE)
      .value("FEED_TYPE_NAV", fastfeed::proto::FEED_TYPE_NAV)
      .value("FEED_TYPE_KLINE", fastfeed::proto::FEED_TYPE_KLINE)
      .value("FEED_TYPE_IV_GREEKS", fastfeed::proto::FEED_TYPE_IV_GREEKS)
      .value("FEED_TYPE_CUSTOM", fastfeed::proto::FEED_TYPE_CUSTOM)
      .value("FEED_TYPE_WALLET", fastfeed::proto::FEED_TYPE_WALLET)
      .value("FEED_TYPE_MARKET", fastfeed::proto::FEED_TYPE_MARKET)
      .export_values();
}

}  // namespace appcoin2::support::feed